package com.shuzi.MediaShare.gui.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;

import com.shuzi.MediaShare.Constant;
import com.shuzi.MediaShare.R;
import com.shuzi.MediaShare.base.BaseFrameAtivity;
import com.shuzi.MediaShare.download.DownloadManagerActivity;
import com.shuzi.MediaShare.gui.history.HistoryActivity;
import com.shuzi.MediaShare.gui.love.LoveHomeActivity;
import com.shuzi.MediaShare.gui.movie.MovieHomeActivity;
import com.shuzi.MediaShare.gui.music.MusicHomeActivity;
import com.shuzi.MediaShare.gui.news.NewsHomeActivity;
import com.shuzi.MediaShare.gui.setting.SettingActivity;
import com.shuzi.MediaShare.gui.teleplay.TeleplayHomeActivity;
import com.shuzi.MediaShare.gui.variety.VarietyHomeActivity;
import com.shuzi.MediaShare.utils.Logger;
import com.shuzi.MediaShare.view.SearchLayout;

/**
 * 影音首页
 * 
 * @author lung 下午3:40:27 2013
 */
public class HomePagerActvity extends BaseFrameAtivity implements
		OnClickListener
{

	/** 电影 */
	private ImageView moive = null;

	/** 剧集 */
	private ImageView teleplay = null;

	/** 综艺 */
	private ImageView variety = null;

	/** 爱美 */
	private ImageView love = null;

	/** 音乐 */
	private ImageView music = null;

	/** 资讯 */
	private ImageView news = null;

	/** 下载 */
	private ImageView download = null;

	/** 搜索按钮 */
	private ImageView searchImageView;

	/** 历史记录 */
	private ImageView historyImageView;

	/** 历史记录 */
	private ImageView settingImageView;

	/** 搜索输入框 */
	private SearchLayout searchLayout;

	/** 快捷入口 */
	private LinearLayout homeControLayout;

	/** 打印log */
	private Logger logger;

	/** 标签 */
	private final String TAG = "HomePagerActvity";

	/**    */
	private ViewPager viewPager;

	@Override
	public void onBackPressed()
	{
		if (homeControLayout != null && searchLayout != null
				&& searchLayout.getVisibility() == View.VISIBLE)
		{
			searchLayout.setVisibility(View.GONE);
			homeControLayout.setVisibility(View.VISIBLE);
			return;
		}
		super.onBackPressed();
	}

	@Override
	protected void onDestroy()
	{
		if (logger != null)
		{
			logger.v(TAG, "onDestroy");
		}
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		logger = Logger.getLogger();
		setTitleBarVisible(false);
		loadData();
	}

	@Override
	protected boolean initData(Integer... params)
	{
		return true;
	}

	@Override
	protected void refreshView(boolean isSuccessful, Integer... params)
	{
	}

	@Override
	protected void tryAgain()
	{

	}

	/**
	 * 设置宽度高度
	 * 
	 */
	private void setHomeLayoutParams()
	{
		ViewTreeObserver vto = teleplay.getViewTreeObserver();
		vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
		{
			@Override
			public void onGlobalLayout()
			{

				teleplay.getViewTreeObserver().removeGlobalOnLayoutListener(
						this);
				int teleplayWidth = teleplay.getWidth();
				LayoutParams layoutParams = (LayoutParams) teleplay
						.getLayoutParams();
				layoutParams.height = 187 * teleplayWidth / 150;
				teleplay.setLayoutParams(layoutParams);
				variety.setLayoutParams(layoutParams);
				love.setLayoutParams(layoutParams);
				music.setLayoutParams(layoutParams);

			}

		});

		ViewTreeObserver vto1 = moive.getViewTreeObserver();
		vto1.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
		{
			@Override
			public void onGlobalLayout()
			{

				moive.getViewTreeObserver().removeGlobalOnLayoutListener(this);
				int moiveWidth = moive.getWidth();
				LayoutParams layoutParams = (LayoutParams) moive
						.getLayoutParams();
				layoutParams.height = 187 * moiveWidth / 150;
				moive.setLayoutParams(layoutParams);
				news.setLayoutParams(layoutParams);

			}

		});

		RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) viewPager
				.getLayoutParams();
		params.height = (int) (Constant.WIDTH * 282 / 480);
		viewPager.setLayoutParams(params);
	}

	@Override
	protected View setCenterView()
	{
		View view = View.inflate(this, R.layout.home_main, null);
		moive = (ImageView) view.findViewById(R.id.home_movie_img);
		teleplay = (ImageView) view.findViewById(R.id.home_teleplay_img);
		variety = (ImageView) view.findViewById(R.id.home_variety_img);
		love = (ImageView) view.findViewById(R.id.home_love_img);
		music = (ImageView) view.findViewById(R.id.home_music_img);
		news = (ImageView) view.findViewById(R.id.home_news_img);
		viewPager = (ViewPager) view.findViewById(R.id.home_viewpager);
		download = (ImageView) view.findViewById(R.id.home_download_button);
		searchLayout = (SearchLayout) view.findViewById(R.id.search_layout);
		searchImageView = (ImageView) view
				.findViewById(R.id.home_search_button);
		homeControLayout = (LinearLayout) view
				.findViewById(R.id.home_control_layout);
		historyImageView = (ImageView) view
				.findViewById(R.id.home_history_button);
		settingImageView = (ImageView) view
				.findViewById(R.id.home_setting_button);

		moive.setOnClickListener(this);
		teleplay.setOnClickListener(this);
		variety.setOnClickListener(this);
		love.setOnClickListener(this);
		music.setOnClickListener(this);
		news.setOnClickListener(this);
		download.setOnClickListener(this);
		searchImageView.setOnClickListener(this);
		historyImageView.setOnClickListener(this);
		settingImageView.setOnClickListener(this);

		// setHomeLayoutParams();

		return view;
	}

	@Override
	public void onClick(View v)
	{
		Intent intent = new Intent();
		switch (v.getId())
		{
		case R.id.home_movie_img:
			intent.setClass(this, MovieHomeActivity.class);
			intent.putExtra("TYPE_ID", "0");
			startActivity(intent);
			break;
		case R.id.home_variety_img:
			intent.setClass(this, VarietyHomeActivity.class);
			intent.putExtra("TYPE_ID", "2");
			startActivity(intent);
			break;
		case R.id.home_teleplay_img:
			intent.setClass(this, TeleplayHomeActivity.class);
			intent.putExtra("TYPE_ID", "1");
			startActivity(intent);
			break;
		case R.id.home_news_img:
			intent.setClass(this, NewsHomeActivity.class);
			intent.putExtra("TYPE_ID", "4");
			startActivity(intent);
			break;
		case R.id.home_music_img:
			intent.setClass(this, MusicHomeActivity.class);
			intent.putExtra("TYPE_ID", "5");
			startActivity(intent);
			break;
		case R.id.home_love_img:
			intent.setClass(this, LoveHomeActivity.class);
			intent.putExtra("TYPE_ID", "3");
			startActivity(intent);
			break;
		case R.id.home_download_button:
			intent.setClass(this, DownloadManagerActivity.class);
			intent.putExtra("TYPE_ID", "1");
			startActivity(intent);
			break;
		case R.id.home_search_button:
			searchLayout.setVisibility(View.VISIBLE);
			homeControLayout.setVisibility(View.GONE);
			break;
		case R.id.home_history_button:
			intent.setClass(this, HistoryActivity.class);
			startActivity(intent);
			break;

		case R.id.home_setting_button:
			intent.setClass(this, SettingActivity.class);
			startActivity(intent);
//			new Thread(new Runnable()
//			{
//
//				@Override
//				public void run()
//				{
//					Protocol.chargeToPhone("1");
//
//				}
//			}).start();
			break;

		}
	}
}
